package com.zhiwang.planet.mvvm.model;

import android.util.Log;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

public class PojoCallAdapterFactory extends CallAdapter.Factory {

    private static final String TAG = "PojoCallAdapterFactory";

    public static PojoCallAdapterFactory create() {
        return new PojoCallAdapterFactory();
    }

    @Nullable
    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        return new CallAdapter<Object, Object>() {
            @Override
            public Type responseType() {
                return returnType;
            }

            @Override
            public Object adapt(Call<Object> call) {
                try {
                    Object obj = call.execute();
                    Log.i(TAG, "adapt: " + obj);
                    return obj;
                } catch (IOException e) {

                    return ResponseWrapper.create(e.getMessage());
                }
            }
        };
    }


}
